package com.zhijiang.hospital.controller;

import com.alibaba.excel.EasyExcel;
import com.zhijiang.hospital.entity.dto.SocietyServiceDataDto;
import com.zhijiang.hospital.mapper.SocietyServiceDataMapper;
import com.zhijiang.hospital.service.SocietyServiceDataService;
import com.zhijiang.util.BaseQuery;
import com.zhijiang.util.PageList;
import com.zhijiang.util.Result;
import com.zhijiang.util.TimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author 75936
 * @version 1.0
 * @project zhijiang_hospital_data
 * @description
 * @date 2023-02-21 14:33:59
 */
@Slf4j
@Api(value = "社会服务运行数据", tags = "社会服务运行数据")
@RestController
@RequestMapping("societyServiceData")
public class SocietyServiceDataController {
    @Autowired
    private SocietyServiceDataService societyServiceDataService;
    @Autowired
    SocietyServiceDataMapper societyServiceDataMapper;

    @ApiOperation("社会服务运行数据分页")
    @ApiModelProperty(value = "社会服务运行数据分页")
    @PostMapping("findSocietyServiceData")
    public Result findSocietyServiceData(@RequestBody BaseQuery baseQuery) {
        PageList<SocietyServiceDataDto> societyServiceData = societyServiceDataService.findSocietyServiceData(TimeUtil.initBaseQuery(baseQuery));
        return Result.ok(societyServiceData);
    }

    @ApiOperation("社会服务运行数据Excel导出")
    @ApiModelProperty(value = "社会服务运行数据Excel导出")
    @GetMapping("loadExcel")
    public void loadExcel(HttpServletResponse response, String startTime, String endTime) throws IOException {
        List<SocietyServiceDataDto> societyServiceData = societyServiceDataMapper.findSocietyServiceData(startTime, endTime);
        log.error("公共运行数据", societyServiceData);
        String title = "公共运行数据";
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("社会服务运行数据.xlsx", "utf-8"));
        //4. 创建输入、输出流
        EasyExcel.write(response.getOutputStream(), SocietyServiceDataDto.class).sheet(title).doWrite(societyServiceData);
    }

    @GetMapping("loadSocietyServiceDataYear")
    @ApiOperation("社会服务运行数据年度数据对比今年")
    @ApiModelProperty(value = "社会服务运行数据年度数据对比今年")
    public Result loadSocietyServiceDataYear() {
        List<SocietyServiceDataDto> societyServiceDataDtos = societyServiceDataService.loadSocietyServiceDataYear();
        return Result.ok(societyServiceDataDtos);
    }

    @GetMapping("loadSocietyServiceDataLastYear")
    @ApiOperation("社会服务运行数据年度数据对比去年")
    @ApiModelProperty(value = "社会服务运行数据年度数据对比去年")
    public Result loadSocietyServiceDataLastYear() {
        List<SocietyServiceDataDto> societyServiceDataDtos = societyServiceDataService.loadSocietyServiceDataLastYear();
        return Result.ok(societyServiceDataDtos);
    }


}
